package com.xsir.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xsir.common.Result;
import com.xsir.dto.CommentList;
import com.xsir.entity.Collection;
import com.xsir.entity.Comment;
import com.xsir.service.ICommentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author xsir
 * @since 2020-11-08
 */
@RestController
@RequestMapping("/comment")
@Slf4j
public class CommentController {
    @Resource
    private ICommentService commentService;
    
    /**
     * 查询文章的评论
     */
    @GetMapping("/article")
    public Result article(Page<Comment> page, @RequestParam("id") Long id) {
        List<CommentList> commentList = commentService.getArticleComment(page, id);
        if(commentList == null) {
            commentList = Collections.emptyList();
        }
        return Result.ok(commentList);
    }
    
    /**
     * 发表评论或修改评论
     */
    @RequiresRoles("user")
    @PostMapping("/article/saveOrUpdate")
    public Result add(@RequestBody Comment comment) {
        log.debug(comment.toString());
        boolean b = commentService.saveOrUpdate(comment);
        return b ? Result.ok() : Result.error();
    }
    
    /**
     * 删除评论
     */
    @RequestMapping("/article/delete")
    public Result delete(@RequestParam("id") Long id) {
        boolean b = commentService.removeById(id);
        return b ? Result.ok() : Result.error();
    }
}
